package com.dyna.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dyna.core.utils.PageUtil;
import com.dyna.domain.req.PageParam;
import com.dyna.domain.resp.PageResult;
import com.dyna.domain.resp.R;
import com.dyna.enums.CommonStatusEnum;
import com.dyna.excel.core.util.ExcelUtil;
import com.dyna.foundation.domain.entity.SysRole;
import com.dyna.foundation.domain.form.SysRoleSaveForm;
import com.dyna.foundation.domain.req.SysRolePageReq;
import com.dyna.foundation.domain.resp.SysRoleResp;
import com.dyna.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;

import static java.util.Collections.singleton;

/**
 * @author zhoucaiwang
 * @date 2023-08-05
 */
@Api(value = "角色")
@RestController
@RequestMapping(value = "/sys-role")
public class SysRoleController {

    private final SysRoleService sysRoleService;

    @Autowired
    public SysRoleController(SysRoleService sysRoleService) {
        this.sysRoleService = sysRoleService;
    }

    @PostMapping("/create")
    @ApiOperation(value = "创建角色")
    public R<Long> createRole(@Valid @RequestBody SysRoleSaveForm createReqVO) {
        return R.data(sysRoleService.createRole(createReqVO, null));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改角色")
    public R<Boolean> updateRole(@Valid @RequestBody SysRoleSaveForm updateReqVO) {
        sysRoleService.updateRole(updateReqVO);
        return R.data(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除角色")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "id", value = "角色编号", required = true, example = "1024")})
    public R<Boolean> deleteRole(@RequestParam("id") Long id) {
        sysRoleService.deleteRole(id);
        return R.data(true);
    }

    @GetMapping("/get")
    @ApiOperation(value = "获得角色信息")
    public R<SysRoleResp> getRole(@RequestParam("id") Long id) {
        SysRole role = sysRoleService.getRole(id);
        return R.data(BeanUtil.toBean(role, SysRoleResp.class));
    }

    @GetMapping("/page")
    @ApiOperation(value = "获得角色分页")
    public R<PageResult<SysRoleResp>> getRolePage(SysRolePageReq pageReqVO) {
        PageResult<SysRole> pageResult = sysRoleService.getRolePage(pageReqVO);
        return R.data(PageUtil.convertPageResult(pageResult, SysRoleResp.class));
    }

    @GetMapping({"/list-all-simple", "/simple-list"})
    @ApiOperation(value = "获取角色精简信息列表", notes = "只包含被开启的角色，主要用于前端的下拉选项")
    public R<List<SysRoleResp>> getSimpleRoleList() {
        List<SysRole> list = sysRoleService.getRoleListByStatus(singleton(CommonStatusEnum.ENABLE.getStatus()));
        list.sort(Comparator.comparing(SysRole::getSort));
        return R.data(PageUtil.convertPageResult(list, SysRoleResp.class));
    }

    @GetMapping("/export-excel")
    @ApiOperation(value = "导出角色 Excel")
    public void export(HttpServletResponse response, @Validated SysRolePageReq exportReqVO) throws IOException {
        exportReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SysRole> list = sysRoleService.getRolePage(exportReqVO).getList();
        // 输出
        ExcelUtil.write(response, "角色数据.xls", "数据", SysRoleResp.class,
                PageUtil.convertPageResult(list, SysRoleResp.class));
    }


}